package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        log.info("shoppingCart;{}",shoppingCart);

        //设置购物车用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //根据用户ID查询数据库购物车信息

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());


        //判断购物车里的数据是菜品还是套餐

        if (dishId!=null){

            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {

            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        ShoppingCart dataCart = shoppingCartService.getOne(queryWrapper);

        log.info("cart2:{}",dataCart);
        //判断该菜品或套餐的数量
        if(dataCart!=null){
            Integer number = dataCart.getNumber();
            log.info("number:{}",number);
            dataCart.setNumber(number+1);
            shoppingCartService.updateById(dataCart);
        }else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            dataCart=shoppingCart;
        }

        log.info("cart1:{}",shoppingCart);
        log.info("cart2:{}",dataCart);

        return R.success(dataCart);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);

    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("删除成功");
    }

    @PostMapping("/sub")
    public  R<ShoppingCart> update(@RequestBody ShoppingCart shoppingCart){

        log.info("shoppingCart:{}",shoppingCart);

        //设置购物车用户ID
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //根据用户ID查询数据库购物车信息

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());


        //判断购物车里的数据是菜品还是套餐

        if (dishId!=null){

            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        }else {

            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());

        }

        ShoppingCart dataCart = shoppingCartService.getOne(queryWrapper);

        log.info("cart2:{}",dataCart);

        //判断该菜品或套餐的数量
        if(dataCart.getNumber()>1){
            Integer number = dataCart.getNumber();
            log.info("number:{}",number);
            dataCart.setNumber(number-1);
            shoppingCartService.updateById(dataCart);
        }else {
            log.info("number:{}",dataCart.getNumber());
            shoppingCartService.remove(queryWrapper);
            dataCart=shoppingCart;
        }

        log.info("cart1:{}",shoppingCart);
        log.info("cart2:{}",dataCart);

        return R.success(dataCart);
    }
}
